Skip to content

zebra: kernel_init function optimizations#22369

Open
sri-mohan1 wants to merge 1 commit into
FRRouting:masterfrom
sri-mohan1:srib-26-zebra-a
Open

zebra: kernel_init function optimizations#22369
sri-mohan1 wants to merge 1 commit into
FRRouting:masterfrom
sri-mohan1:srib-26-zebra-a

Conversation

@sri-mohan1

Copy link
Copy Markdown
Contributor

these changes are for improving the code maintainability and readability

@greptile-apps

greptile-apps Bot commented Jun 15, 2026

Copy link
Copy Markdown

Greptile Summary

This PR refactors kernel_init in zebra/kernel_netlink.c by extracting three static helpers (netlink_set_nonblock, kernel_init_nlsock, netlink_enable_ext_ack) and introducing a RTNLGRP_BIT macro to replace the inline bit-shift expressions for multicast group bitmasks. The underlying socket creation and configuration behavior is preserved faithfully.

  • RTNLGRP_BIT(g) centralizes the (uint32_t)1 << (g-1) pattern with a comment explaining the 1-based RTNLGRP convention and the < 32 limit.
  • kernel_init_nlsock consolidates the five-step socket init pattern (name format, set sock=-1, create, log failure, hash-insert), with a warn_only flag to distinguish the optional ge_netlink_cmd socket from the four critical ones.
  • netlink_enable_ext_ack deduplicates the setsockopt(SOL_NETLINK, NETLINK_EXT_ACK, ...) calls for the command and dataplane-out sockets; the generic-netlink socket's distinct flog_err path is intentionally kept inline.
  • netlink_set_nonblock unifies the fcntl(F_SETFL, O_NONBLOCK) calls under flog_err_sys, which is more appropriate than the original mix of flog_err_sys/flog_err.

Confidence Score: 3/5

The refactoring is logically correct, but kernel_init_nlsock passes a non-literal name_fmt to snprintf, which will fire the unconditionally-enabled -Wformat-nonliteral build flag and break CI where --enable-werror is active.

The socket initialization logic and group bitmask computation are faithfully preserved. The one concrete defect is in kernel_init_nlsock: snprintf(nl->name, sizeof(nl->name), name_fmt, ns_id) uses a runtime const char * as the format argument. FRR's configure.ac unconditionally adds -Wformat-nonliteral to the compiler flags, producing a warning on every developer build and a hard error on CI that runs --enable-werror. The fix is small, but the build breakage makes it worth resolving before merge.

zebra/kernel_netlink.c — specifically kernel_init_nlsock and its snprintf call with the non-literal name_fmt parameter.

Important Files Changed

Filename Overview
zebra/kernel_netlink.c Refactors kernel_init by extracting three helpers and introducing RTNLGRP_BIT; functional behavior is preserved, but kernel_init_nlsock passes a non-literal format string to snprintf, which will fire the unconditionally-enabled -Wformat-nonliteral build flag.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[kernel_init] --> B[Compute groups / dplane_groups / ext_groups bitmasks]
    B --> C[kernel_init_nlsock: netlink-listen\ngroups + ext_groups, fatal]
    C -->|fail| X1[frr_exit_with_buffer_flush]
    C -->|ok| D[kernel_init_nlsock: netlink-cmd\nno groups, fatal]
    D -->|fail| X2[frr_exit_with_buffer_flush]
    D -->|ok| E[kernel_init_nlsock: netlink-dp\nno groups, fatal]
    E -->|fail| X3[frr_exit_with_buffer_flush]
    E -->|ok| F[kernel_init_nlsock: netlink-dp-in\ndplane_groups, fatal]
    F -->|fail| X4[frr_exit_with_buffer_flush]
    F -->|ok| G[kernel_init_nlsock: generic-netlink-cmd\nno groups, warn-only]
    G --> H{SOL_NETLINK defined?}
    H -->|yes| I[setsockopt RTNLGRP_BRVLAN on dp-in]
    I --> J[netlink_enable_ext_ack on cmd + dp-out]
    J --> K{ge_netlink_cmd sock >= 0?}
    K -->|yes| L[setsockopt NETLINK_EXT_ACK on ge_netlink_cmd]
    L --> M[setsockopt NETLINK_CAP_ACK on dp-out]
    K -->|no| M
    M --> N[endif SOL_NETLINK]
    H -->|no| N
    N --> O[netlink_set_nonblock on all sockets]
    O --> P{rcvbufsize set?}
    P -->|yes| Q[netlink_recvbuf on all sockets]
    P -->|no| R[netlink_install_filter on listen + dp-in]
    Q --> R
    R --> S[event_add_read for kernel_read]
    S --> T[rt_netlink_init + ge_netlink_init]
Loading
Prompt To Fix All With AI
Fix the following 2 code review issues. Work through them one at a time, proposing concise fixes.

---

### Issue 1 of 2
zebra/kernel_netlink.c:1661
**Non-literal format string triggers `-Wformat-nonliteral`**

`configure.ac` unconditionally enables `-Wformat-nonliteral`, so passing `name_fmt` (a `const char *` parameter, not a string literal) as the format argument to `snprintf` will produce a compiler warning in every FRR build, and a hard build failure on CI where `--enable-werror` is used. Either annotate the parameter with `__attribute__((format(printf, 2, 0)))` / `PRINTFRR(2, 0)`, or replace the single `snprintf` with a call that makes the format an inline literal.

### Issue 2 of 2
zebra/kernel_netlink.c:81-84
Double blank line after the macro definition — FRR style uses a single blank line between top-level declarations.

```suggestion
#define RTNLGRP_BIT(g) ((uint32_t)1 << ((g) - 1))

static const struct message nlmsg_str[] = {
```

Reviews (1): Last reviewed commit: "zebra: kernel_init function optimization..." | Re-trigger Greptile

Comment thread zebra/kernel_netlink.c Outdated
uint8_t ext_group_size, ns_id_t ns_id,
int nl_family, bool warn_only)
{
snprintf(nl->name, sizeof(nl->name), name_fmt, ns_id);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Non-literal format string triggers -Wformat-nonliteral

configure.ac unconditionally enables -Wformat-nonliteral, so passing name_fmt (a const char * parameter, not a string literal) as the format argument to snprintf will produce a compiler warning in every FRR build, and a hard build failure on CI where --enable-werror is used. Either annotate the parameter with __attribute__((format(printf, 2, 0))) / PRINTFRR(2, 0), or replace the single snprintf with a call that makes the format an inline literal.

Prompt To Fix With AI
This is a comment left during a code review.
Path: zebra/kernel_netlink.c
Line: 1661

Comment:
**Non-literal format string triggers `-Wformat-nonliteral`**

`configure.ac` unconditionally enables `-Wformat-nonliteral`, so passing `name_fmt` (a `const char *` parameter, not a string literal) as the format argument to `snprintf` will produce a compiler warning in every FRR build, and a hard build failure on CI where `--enable-werror` is used. Either annotate the parameter with `__attribute__((format(printf, 2, 0)))` / `PRINTFRR(2, 0)`, or replace the single `snprintf` with a call that makes the format an inline literal.

How can I resolve this? If you propose a fix, please make it concise.

Comment thread zebra/kernel_netlink.c Outdated
@sri-mohan1 sri-mohan1 force-pushed the srib-26-zebra-a branch 3 times, most recently from 16b8c8c to b10e14f Compare June 16, 2026 04:36
@sri-mohan1

Copy link
Copy Markdown
Contributor Author

Greptile Summary

This PR refactors kernel_init in zebra/kernel_netlink.c by extracting three static helpers (netlink_set_nonblock, kernel_init_nlsock, netlink_enable_ext_ack) and introducing a RTNLGRP_BIT macro to replace the inline bit-shift expressions for multicast group bitmasks. The underlying socket creation and configuration behavior is preserved faithfully.

  • RTNLGRP_BIT(g) centralizes the (uint32_t)1 << (g-1) pattern with a comment explaining the 1-based RTNLGRP convention and the < 32 limit.
  • kernel_init_nlsock consolidates the five-step socket init pattern (name format, set sock=-1, create, log failure, hash-insert), with a warn_only flag to distinguish the optional ge_netlink_cmd socket from the four critical ones.
  • netlink_enable_ext_ack deduplicates the setsockopt(SOL_NETLINK, NETLINK_EXT_ACK, ...) calls for the command and dataplane-out sockets; the generic-netlink socket's distinct flog_err path is intentionally kept inline.
  • netlink_set_nonblock unifies the fcntl(F_SETFL, O_NONBLOCK) calls under flog_err_sys, which is more appropriate than the original mix of flog_err_sys/flog_err.

Confidence Score: 3/5

The refactoring is logically correct, but kernel_init_nlsock passes a non-literal name_fmt to snprintf, which will fire the unconditionally-enabled -Wformat-nonliteral build flag and break CI where --enable-werror is active.

The socket initialization logic and group bitmask computation are faithfully preserved. The one concrete defect is in kernel_init_nlsock: snprintf(nl->name, sizeof(nl->name), name_fmt, ns_id) uses a runtime const char * as the format argument. FRR's configure.ac unconditionally adds -Wformat-nonliteral to the compiler flags, producing a warning on every developer build and a hard error on CI that runs --enable-werror. The fix is small, but the build breakage makes it worth resolving before merge.

zebra/kernel_netlink.c — specifically kernel_init_nlsock and its snprintf call with the non-literal name_fmt parameter.

Important Files Changed

Filename Overview
zebra/kernel_netlink.c Refactors kernel_init by extracting three helpers and introducing RTNLGRP_BIT; functional behavior is preserved, but kernel_init_nlsock passes a non-literal format string to snprintf, which will fire the unconditionally-enabled -Wformat-nonliteral build flag.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[kernel_init] --> B[Compute groups / dplane_groups / ext_groups bitmasks]
    B --> C[kernel_init_nlsock: netlink-listen\ngroups + ext_groups, fatal]
    C -->|fail| X1[frr_exit_with_buffer_flush]
    C -->|ok| D[kernel_init_nlsock: netlink-cmd\nno groups, fatal]
    D -->|fail| X2[frr_exit_with_buffer_flush]
    D -->|ok| E[kernel_init_nlsock: netlink-dp\nno groups, fatal]
    E -->|fail| X3[frr_exit_with_buffer_flush]
    E -->|ok| F[kernel_init_nlsock: netlink-dp-in\ndplane_groups, fatal]
    F -->|fail| X4[frr_exit_with_buffer_flush]
    F -->|ok| G[kernel_init_nlsock: generic-netlink-cmd\nno groups, warn-only]
    G --> H{SOL_NETLINK defined?}
    H -->|yes| I[setsockopt RTNLGRP_BRVLAN on dp-in]
    I --> J[netlink_enable_ext_ack on cmd + dp-out]
    J --> K{ge_netlink_cmd sock >= 0?}
    K -->|yes| L[setsockopt NETLINK_EXT_ACK on ge_netlink_cmd]
    L --> M[setsockopt NETLINK_CAP_ACK on dp-out]
    K -->|no| M
    M --> N[endif SOL_NETLINK]
    H -->|no| N
    N --> O[netlink_set_nonblock on all sockets]
    O --> P{rcvbufsize set?}
    P -->|yes| Q[netlink_recvbuf on all sockets]
    P -->|no| R[netlink_install_filter on listen + dp-in]
    Q --> R
    R --> S[event_add_read for kernel_read]
    S --> T[rt_netlink_init + ge_netlink_init]
Loading

Prompt To Fix All With AI

Fix the following 2 code review issues. Work through them one at a time, proposing concise fixes.

---

### Issue 1 of 2
zebra/kernel_netlink.c:1661
**Non-literal format string triggers `-Wformat-nonliteral`**

`configure.ac` unconditionally enables `-Wformat-nonliteral`, so passing `name_fmt` (a `const char *` parameter, not a string literal) as the format argument to `snprintf` will produce a compiler warning in every FRR build, and a hard build failure on CI where `--enable-werror` is used. Either annotate the parameter with `__attribute__((format(printf, 2, 0)))` / `PRINTFRR(2, 0)`, or replace the single `snprintf` with a call that makes the format an inline literal.

### Issue 2 of 2
zebra/kernel_netlink.c:81-84
Double blank line after the macro definition — FRR style uses a single blank line between top-level declarations.

```suggestion
#define RTNLGRP_BIT(g) ((uint32_t)1 << ((g) - 1))

static const struct message nlmsg_str[] = {

Reviews (1): Last reviewed commit: ["zebra: kernel_init function optimization..."](https://github.com/frrouting/frr/commit/8a3b37630f043587c31cd5760dd15a4bb0f24ed7) | [Re-trigger Greptile](https://app.greptile.com/api/retrigger?id=37595639)

Corrected the reported items.

@ramanjiee ramanjiee left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes are fine, improving the readability and the code is optimized.

Comment thread zebra/kernel_netlink.c
Comment thread zebra/kernel_netlink.c Outdated
@sri-mohan1 sri-mohan1 force-pushed the srib-26-zebra-a branch 2 times, most recently from 1775a78 to b48c972 Compare June 18, 2026 04:52
@mergify

mergify Bot commented Jun 19, 2026

Copy link
Copy Markdown

Tick the box to add this pull request to the merge queue (same as @mergifyio queue).

  • Queue this pull request

@github-actions github-actions Bot added the rebase PR needs rebase label Jun 23, 2026
these changes are for improving the code maintainability and readability

Signed-off-by: sri-mohan1 <sri.mohan@samsung.com>
@sri-mohan1

Copy link
Copy Markdown
Contributor Author

Review comments addressed and Rebased onto latest master.
Kindly review further and take it forward.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants